package com.forevershine.business.report;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;
import net.sf.jasperreports.engine.util.JRLoader;

import org.apache.commons.io.IOUtils;

import com.forevershine.business.report.model.SampleNoteReport;

public class PDFReportExporter {

	public static void export(String reportTemplatePath, String outputPath, Object... reportBeans) throws IOException {
		JRBeanArrayDataSource jrds = null;
		JasperPrint jasperPrint = null;
		Map reportParams = new HashMap();

		try {
			JasperReport jasperReport = (JasperReport) JRLoader
					.loadObjectFromFile(reportTemplatePath);
			List list = new ArrayList();
			
			for(Object reportBean :reportBeans )
			{
				list.add(reportBean);
			} 
			
			jrds = new JRBeanArrayDataSource(list.toArray());
			File file = new File(outputPath);
			FileOutputStream fos = new FileOutputStream(file);
			IOUtils.write(JasperRunManager.runReportToPdf(jasperReport,
					reportParams, jrds), fos);
			fos.flush();

		} catch (JRException e) {
			e.printStackTrace();
		}
	}
	
	public  static void main(String[] args) throws IOException
	{
		SampleNoteReport report = new SampleNoteReport();
		report.setUser("thomas jefferson");
		report.setIssuedate(new Date());	
		
		export("D:/forevershine/foe/SampleNoteReport.jasper", "c:/sampleNoteReport.pdf", report) ;
	}

}
